#include "precompiled/pchnscene.h"
//------------------------------------------------------------------------------
//  ngeometrynode_cmds.cc
//  (C) 2004 Conjurer Services, S.A.
//------------------------------------------------------------------------------
#include "nscene/ngeometrynode.h"
#include "kernel/npersistserver.h"
#include "kernel/ndependencyserver.h"

static void n_setsurface(void* slf, nCmd* cmd);
static void n_getsurface(void* slf, nCmd* cmd);
static void n_setworldcoord(void* slf, nCmd* cmd);
static void n_getworldcoord(void* slf, nCmd* cmd);

//------------------------------------------------------------------------------
/**
    @scriptclass
    ngeometrynode

    @cppclass
    nGeometryNode
    
    @superclass
    nabstractshadernode

    @classinfo
    This one can render a simple static mesh.
*/
void
n_initcmds_nGeometryNode(nClass* cl)
{
    cl->BeginCmds();
    cl->AddCmd("v_setsurface_s",        'SSFC', n_setsurface);
    cl->AddCmd("s_getsurface_v",        'GSFC', n_getsurface);
    cl->AddCmd("v_setworldcoord_b",     'SWCD', n_setworldcoord);
    cl->AddCmd("b_getworldcoord_v",     'GWCD', n_getworldcoord);
    cl->EndCmds();
}

//------------------------------------------------------------------------------
/**
    @cmd
    setsurface
    @input
    s(surface node)
    @output
    v
    @info
    Set the surface for the mesh.
*/
static void
n_setsurface(void* slf, nCmd* cmd)
{
    nGeometryNode* self = (nGeometryNode*) slf;
    self->SetSurface(cmd->In()->GetS());
}

//------------------------------------------------------------------------------
/**
    @cmd
    getsurface
    @input
    v
    @output
    s(surface node)
    @info
    Gets the surface for the geometry.
*/
static void
n_getsurface(void* slf, nCmd* cmd)
{
    nGeometryNode* self = (nGeometryNode*) slf;
    cmd->Out()->SetS(self->GetSurface());
}

//------------------------------------------------------------------------------
/**
*/
static void
n_setworldcoord(void* slf, nCmd* cmd)
{
    nGeometryNode* self = (nGeometryNode*) slf;
    self->SetWorldCoord(cmd->In()->GetB());
}

//------------------------------------------------------------------------------
/**
*/
static void
n_getworldcoord(void* slf, nCmd* cmd)
{
    nGeometryNode* self = (nGeometryNode*) slf;
    cmd->Out()->SetB(self->GetWorldCoord());
}

//------------------------------------------------------------------------------
/**
*/
bool
nGeometryNode::SaveCmds(nPersistServer* ps)
{
    if (nAbstractShaderNode::SaveCmds(ps))
    {
        nCmd* cmd;
        nDependencyServer *depServer = ps->GetDependencyServer("ngeometrynode", "setsurface");

        // --- setsurface ---
        if (this->refSurfaceNode.isvalid())
        {
            if (depServer && ps->GetSaveMode() == nPersistServer::SAVEMODE_FOLD)
            {
                cmd = ps->GetCmd(this, 'SDCY');
                nString path;
                depServer->SaveObject((nObject *) this->refSurfaceNode.get(), "setsurface", path);
                cmd->In()->SetS(path.Get());
                cmd->In()->SetS(depServer->GetFullName().Get());
                cmd->In()->SetS("setsurface");
                ps->PutCmd(cmd);
            }
            else
            {
                cmd = ps->GetCmd(this, 'SSFC');
                cmd->In()->SetS(this->refSurfaceNode.getname());
                ps->PutCmd(cmd);
            }
        }

        // --- setworldcoord ---
        if (this->GetWorldCoord())
        {
            cmd = ps->GetCmd(this, 'SWCD');
            cmd->In()->SetB(this->GetWorldCoord());
            ps->PutCmd(cmd);
        }

        return true;
    }
    return false;
}
